package com.ccys.cms.interceptor;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.ccys.common.constants.ContextConstants;
import com.ccys.common.enums.CodeEnum;
import com.ccys.common.util.JedisUtil;
import com.ccys.common.util.Kit;
import com.ccys.db.cache.UserUtil;
import com.ccys.db.entity.TblAccount;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.http.server.reactive.ServerHttpRequest;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.text.DateFormat;
import java.util.Arrays;
import java.util.Date;
import java.util.Map;


/**
 * @description 校验拦截器
 * @date     2023/12/11
 * @author   cloud fly
 */
@Slf4j
public class CheckInterceptor implements HandlerInterceptor{


 @Override
 public void afterCompletion(HttpServletRequest arg0,
                             HttpServletResponse arg1, Object arg2, Exception arg3)
         throws Exception {

 }

 @Override
 public void postHandle(HttpServletRequest arg0, HttpServletResponse arg1,
                        Object arg2, ModelAndView arg3) throws Exception {

 }

 @Override
 public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
     String token = request.getHeader("token");
     Object object = JedisUtil.getObject(token, CodeEnum.REDIS_USER.codeOf());
     if (StringUtils.isBlank(token) || object == null) {
         return true;
     }
     JedisUtil.setexObj(token,ContextConstants.DEFAULT_NOT_OPERATED_TIME, JSONObject.toJSON(object), CodeEnum.REDIS_USER.codeOf());
     TblAccount tblAccount = JSONObject.parseObject(JSONObject.toJSONString(object), TblAccount.class);
     if(tblAccount != null) {
         JedisUtil.setex(tblAccount.getId(),ContextConstants.DEFAULT_NOT_OPERATED_TIME, token, CodeEnum.REDIS_USER.codeOf());
     }
     return true;
 }
}
